package com.ycy.chuwenuniapp.exception;


import com.ycy.chuwenuniapp.common.ResData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

// 异常处理类 MyExceptionHandler
@ControllerAdvice  //Controller增强器,作用是给Controller控制器添加统一的操作或处理。
public class MyExceptionHandler {
    // @ExceptionHandler(CustomException.class)  //设置处理的自定义类
    @ExceptionHandler({CustomException.class})
    @ResponseBody  //将方法的返回值作为响应体参数
    public ResData<String> handle(CustomException customException) {
        //返回的格式： { code: 403,message: "无权限访问(resCodeEnum里的message)",data: "用户名错误（抛出异常时自定义的语句）"}
        return new ResData<>(customException.getResCodeEnum(),customException.getMessage(),customException.getData());
    }
}
